home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000006.txt < prev    next >
Text File  |  2013-04-03  |  7KB  |  271 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // extension_apitest.js
  6. // mini-framework for ExtensionApiTest browser tests
  7.  
  8.   chrome.test = chrome.test || {};
  9.  
  10.   chrome.test.tests = chrome.test.tests || [];
  11.  
  12.   var currentTest = null;
  13.   var lastTest = null;
  14.   var testsFailed = 0;
  15.   var testCount = 1;
  16.   var failureException = 'chrome.test.failure';
  17.  
  18.   // Helper function to get around the fact that function names in javascript
  19.   // are read-only, and you can't assign one to anonymous functions.
  20.   function testName(test) {
  21.     return test ? (test.name || test.generatedName) : "(no test)";
  22.   }
  23.  
  24.   function testDone() {
  25.     // Use setTimeout here to allow previous test contexts to be
  26.     // eligible for garbage collection.
  27.     setTimeout(chrome.test.runNextTest, 0);
  28.   }
  29.  
  30.   function allTestsDone() {
  31.     if (testsFailed == 0) {
  32.       chrome.test.notifyPass();
  33.     } else {
  34.       chrome.test.notifyFail('Failed ' + testsFailed + ' of ' +
  35.                              testCount + ' tests');
  36.     }
  37.  
  38.     // Try to get the script to stop running immediately.
  39.     // This isn't an error, just an attempt at saying "done".
  40.     throw "completed";
  41.   }
  42.  
  43.   var pendingCallbacks = 0;
  44.  
  45.   chrome.test.callbackAdded = function() {
  46.     pendingCallbacks++;
  47.  
  48.     return function() {
  49.       pendingCallbacks--;
  50.       if (pendingCallbacks == 0) {
  51.         chrome.test.succeed();
  52.       }
  53.     };
  54.   };
  55.  
  56.   chrome.test.runNextTest = function() {
  57.     // There may have been callbacks which were interrupted by failure
  58.     // exceptions.
  59.     pendingCallbacks = 0;
  60.  
  61.     lastTest = currentTest;
  62.     currentTest = chrome.test.tests.shift();
  63.  
  64.     if (!currentTest) {
  65.       allTestsDone();
  66.       return;
  67.     }
  68.  
  69.     try {
  70.       chrome.test.log("( RUN      ) " + testName(currentTest));
  71.       currentTest.call();
  72.     } catch (e) {
  73.       if (e !== failureException)
  74.         chrome.test.fail('uncaught exception: ' + e);
  75.     }
  76.   };
  77.  
  78.   chrome.test.fail = function(message) {
  79.     chrome.test.log("(  FAILED  ) " + testName(currentTest));
  80.  
  81.     var stack = {};
  82.     Error.captureStackTrace(stack, chrome.test.fail);
  83.  
  84.     if (!message)
  85.       message = "FAIL (no message)";
  86.  
  87.     message += "\n" + stack.stack;
  88.     console.log("[FAIL] " + testName(currentTest) + ": " + message);
  89.     testsFailed++;
  90.     testDone();
  91.  
  92.     // Interrupt the rest of the test.
  93.     throw failureException;
  94.   };
  95.  
  96.   chrome.test.succeed = function() {
  97.     console.log("[SUCCESS] " + testName(currentTest));
  98.     chrome.test.log("(  SUCCESS )");
  99.     testDone();
  100.   };
  101.  
  102.   chrome.test.assertTrue = function(test, message) {
  103.     chrome.test.assertBool(test, true, message);
  104.   };
  105.  
  106.   chrome.test.assertFalse = function(test, message) {
  107.     chrome.test.assertBool(test, false, message);
  108.   };
  109.  
  110.   chrome.test.assertBool = function(test, expected, message) {
  111.     if (test !== expected) {
  112.       if (typeof(test) == "string") {
  113.         if (message)
  114.           message = test + "\n" + message;
  115.         else
  116.           message = test;
  117.       }
  118.       chrome.test.fail(message);
  119.     }
  120.   };
  121.  
  122.   chrome.test.checkDeepEq = function (expected, actual) {
  123.     if ((expected === null) != (actual === null))
  124.       return false;
  125.  
  126.     if (expected === actual)
  127.       return true;
  128.  
  129.     if (typeof(expected) !== typeof(actual))
  130.       return false;
  131.  
  132.     for (var p in actual) {
  133.       if (actual.hasOwnProperty(p) && !expected.hasOwnProperty(p))
  134.         return false;
  135.     }
  136.     for (var p in expected) {
  137.       if (expected.hasOwnProperty(p) && !actual.hasOwnProperty(p))
  138.         return false;
  139.     }
  140.  
  141.     for (var p in expected) {
  142.       var eq = true;
  143.       switch (typeof(expected[p])) {
  144.         case 'object':
  145.           eq = chrome.test.checkDeepEq(expected[p], actual[p]);
  146.           break;
  147.         case 'function':
  148.           eq = (typeof(actual[p]) != 'undefined' &&
  149.                 expected[p].toString() == actual[p].toString());
  150.           break;
  151.         default:
  152.           eq = (expected[p] == actual[p] &&
  153.                 typeof(expected[p]) == typeof(actual[p]));
  154.           break;
  155.       }
  156.       if (!eq)
  157.         return false;
  158.     }
  159.     return true;
  160.   };
  161.  
  162.   chrome.test.assertEq = function(expected, actual, message) {
  163.     var error_msg = "API Test Error in " + testName(currentTest);
  164.     if (message)
  165.       error_msg += ": " + message;
  166.     if (typeof(expected) == 'object') {
  167.       if (!chrome.test.checkDeepEq(expected, actual)) {
  168.         chrome.test.fail(error_msg +
  169.                          "\nActual: " + JSON.stringify(actual) +
  170.                          "\nExpected: " + JSON.stringify(expected));
  171.       }
  172.       return;
  173.     }
  174.     if (expected != actual) {
  175.       chrome.test.fail(error_msg +
  176.                        "\nActual: " + actual + "\nExpected: " + expected);
  177.     }
  178.     if (typeof(expected) != typeof(actual)) {
  179.       chrome.test.fail(error_msg +
  180.                        " (type mismatch)\nActual Type: " + typeof(actual) +
  181.                        "\nExpected Type:" + typeof(expected));
  182.     }
  183.   };
  184.  
  185.   chrome.test.assertNoLastError = function() {
  186.     if (chrome.runtime.lastError != undefined) {
  187.       chrome.test.fail("lastError.message == " +
  188.                        chrome.runtime.lastError.message);
  189.     }
  190.   };
  191.  
  192.   chrome.test.assertLastError = function(expectedError) {
  193.     chrome.test.assertEq(typeof(expectedError), 'string');
  194.     chrome.test.assertTrue(chrome.runtime.lastError != undefined,
  195.         "No lastError, but expected " + expectedError);
  196.     chrome.test.assertEq(expectedError, chrome.runtime.lastError.message);
  197.   }
  198.  
  199.   function safeFunctionApply(func, arguments) {
  200.     try {
  201.       if (func)
  202.         func.apply(null, arguments);
  203.     } catch (e) {
  204.       var msg = "uncaught exception " + e;
  205.       chrome.test.fail(msg);
  206.     }
  207.   };
  208.  
  209.   // Wrapper for generating test functions, that takes care of calling
  210.   // assertNoLastError() and (optionally) succeed() for you.
  211.   chrome.test.callback = function(func, expectedError) {
  212.     if (func) {
  213.       chrome.test.assertEq(typeof(func), 'function');
  214.     }
  215.     var callbackCompleted = chrome.test.callbackAdded();
  216.  
  217.     return function() {
  218.       if (expectedError == null) {
  219.         chrome.test.assertNoLastError();
  220.       } else {
  221.         chrome.test.assertLastError(expectedError);
  222.       }
  223.  
  224.       if (func) {
  225.         safeFunctionApply(func, arguments);
  226.       }
  227.  
  228.       callbackCompleted();
  229.     };
  230.   };
  231.  
  232.   chrome.test.listenOnce = function(event, func) {
  233.     var callbackCompleted = chrome.test.callbackAdded();
  234.     var listener = function() {
  235.       event.removeListener(listener);
  236.       safeFunctionApply(func, arguments);
  237.       callbackCompleted();
  238.     };
  239.     event.addListener(listener);
  240.   };
  241.  
  242.   chrome.test.listenForever = function(event, func) {
  243.     var callbackCompleted = chrome.test.callbackAdded();
  244.  
  245.     var listener = function() {
  246.       safeFunctionApply(func, arguments);
  247.     };
  248.  
  249.     var done = function() {
  250.       event.removeListener(listener);
  251.       callbackCompleted();
  252.     };
  253.  
  254.     event.addListener(listener);
  255.     return done;
  256.   };
  257.  
  258.   chrome.test.callbackPass = function(func) {
  259.     return chrome.test.callback(func);
  260.   };
  261.  
  262.   chrome.test.callbackFail = function(expectedError, func) {
  263.     return chrome.test.callback(func, expectedError);
  264.   };
  265.  
  266.   chrome.test.runTests = function(tests) {
  267.     chrome.test.tests = tests;
  268.     testCount = chrome.test.tests.length;
  269.     chrome.test.runNextTest();
  270.   };
  271.